package com.dy.placeOrder.controller;



import com.dy.placeOrder.service.ReceivingAddressService;
import com.dy.placeOrder2.service.OrdersService;
import com.kaoshi.common.config.Result;
import org.springframework.web.bind.annotation.*;

import com.dy.pojo.Orders;
import com.dy.placeOrder.ReceivingAddress;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-20
 */
@RestController
@RequestMapping("placeOrder/receiving-address")
public class ReceivingAddressController {
    @Resource
    private ReceivingAddressService receivingAddressService;
    @Resource
    private OrdersService ordersService;



    //查询用户的收货地址
    @RequestMapping("find")
    public List<ReceivingAddress> find(@RequestParam Integer uid) {
        return receivingAddressService.find(uid);
    }

    @RequestMapping("change")
    public Result change(@RequestBody ReceivingAddress address) {
        if (address.getDefaultAddress() == 1){
            for (ReceivingAddress receivingAddress : receivingAddressService.list()) {
                if (receivingAddress.getUid()==address.getUid()){
                    if (receivingAddress.getDefaultAddress() == 1) {
                        receivingAddress.setDefaultAddress(0);
                        receivingAddressService.saveOrUpdate(receivingAddress);
                    }
                }
//                if (receivingAddress.getDefaultAddress() == 1) {
//                    receivingAddress.setDefaultAddress(0);
//                    receivingAddressService.saveOrUpdate(receivingAddress);
//                }
            }
        }
        receivingAddressService.saveOrUpdate(address);
        return Result.ok();
    }


    //订单确认页面获取收货地址
    @RequestMapping("finddd")
    public List<ReceivingAddress> finddd(@RequestParam Integer id) {
        return receivingAddressService.finddd(id);
    }


    //获取用户的收货地址列表
//    @GetMapping("addresses")
//    public List<receivingAddress> getAddresses() {
//        return receivingAddressService.findAllAddresses(); // 确保返回的地址列表中包含 defaultAddress 字段
//    }

    //获取订单信息
    @GetMapping("findOrder")
    public Orders findOrder(@RequestParam Integer id) {
        return ordersService.getById(id);
    }

    @GetMapping("findAllOrders")
    public List<Orders> findAllOrders() {
        return ordersService.listAll();
    }
}
